﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyMvc.IDAL;
using MyMvc.IBLL;
using MyMvc.Model.Models;
using MyMvc.DAL;
using MyMvc.Model;

namespace MyMvc.BLL
{
    public partial class ArticleService : BaseService<Article>, IArticleService
    {
        public IQueryable<Article> LoadSearchData(GetModelQuery query)
        {
            //拿到所有数据
            var temp = _dbSession.ArticleRepository.LoadEntities(u => true);
            query.total = temp.Count();
            return temp.Skip(query.pageSize * (query.pageIndex - 1)).Take(query.pageSize).AsQueryable();
        }

      
        public int DeleteArticle(List<int> DeleteID)
        {
            foreach (var ID in DeleteID)
            {
                _dbSession.ArticleRepository.DeleteEntities(new Article() { ID = ID });
            }
            return _dbSession.SaveChanges();
        }


        public bool AboutArticle(int articleID, List<int> articleIDList,List<string> namelist)
        {
            //首先根据文章ID，查询出文章信息
            var currentArticle = _dbSession.ArticleRepository.LoadEntities(c => c.ID == articleID).FirstOrDefault();
            if (currentArticle == null)
            {
                return false;
            }
            //得到文章表中的数据全部返回
            var listArticles = currentArticle.AboutArticles.ToList();
            ///处理清空原来的数据，中间表信息
            for (int i = 0; i < listArticles.Count; i++)
            {
                _dbSession.AboutArticleRepository.DeleteEntities(listArticles[i]);
            }
            //真正的删除了所有的数据
            _dbSession.SaveChanges();

            List<int> idlist = new List<int>();
            List<string> titlelist = new List<string>();
            foreach (var id in articleIDList)
            {
                idlist.Add(id);
            }
            foreach (var title in namelist)
            {
                titlelist.Add(title);
            }
            for (int i = 0;i<idlist.Count; i++)
            {
                AboutArticle aboutart = new AboutArticle();
                aboutart.ArticleID = articleID;
                aboutart.AboutID=idlist[i];
                aboutart.AboutName = titlelist[i];
                _dbSession.AboutArticleRepository.AddEntities(aboutart);
            }
                ////在此重新将数据加载会数据库
                //foreach (var articleID1 in articleIDList)
                //{
                //    AboutArticle aboutart = new AboutArticle();
                //    aboutart.ArticleID = articleID;
                //    aboutart.AboutID = articleID1;
                //    _dbSession.AboutArticleRepository.AddEntities(aboutart);
                //}
                //foreach(var name in namelist)
                //{
                //    AboutArticle aboutarticle = new AboutArticle();
                //    aboutarticle.ArticleID = articleID;
                //    aboutarticle.AboutName = name;
                //    _dbSession.AboutArticleRepository.AddEntities(aboutarticle);
                //}
                //实现添加功能
                _dbSession.SaveChanges();
            return true;
        }
    }

}
